package com.example.jetpack

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.paging.PagedListAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView

class RecyclerPagingAdapter
    : PagedListAdapter<Student, MyViewHolder>(object : DiffUtil.ItemCallback<Student>(){
    /**
     * 一般是比较 唯一性的内容
     */
    override fun areItemsTheSame(oldItem: Student, newItem: Student): Boolean {
        return oldItem.id == newItem.id
    }

    /**
     * 对象本身的比较
     */
    override fun areContentsTheSame(oldItem: Student, newItem: Student): Boolean {
        return oldItem == newItem
    }

}) {

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val item = getItem(position)
        item?.let {
            holder.tv.text = "${it.id}${it.name}"
        }?:let {
            holder.tv.text = "数据加载中..."
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item,parent,false)
        return MyViewHolder(view)
    }

}

class MyViewHolder(view: View): RecyclerView.ViewHolder(view){
    val tv : TextView

    init {
        tv = view.findViewById(R.id.tv)
    }

}